package com.template;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bean.Computer;
import com.bean.NginxService;
import com.componentbean.UserNginxSet;

@Component
public class NginxConfig {
	private static final String GAP = " ";
	
	@Autowired
	UserNginxSet userset;
	
	public List<String> getTableMsg(){
		List<String> result = new ArrayList<>();
		result.add("#本机IP列表");
		result.add("<IP>");
		result.add(userset.getComputerSelf().getMySelfDefault().getIp());
		Map<Integer, Computer> list = userset.getComputerSelf().getOther();
		if (null!= list) {
			Iterator<Entry<Integer, Computer>> it = list.entrySet().iterator();
			while (it.hasNext()) {
				result.add(it.next().getValue().getIp());
			}
		}
		result.add("");
		result.add("");
		
		result.add("#其他IP列表");
		result.add("<IP              主机              标签>");
		list = userset.getComputerList().getAll();
		if (null!= list) {
			Iterator<Entry<Integer, Computer>> it = list.entrySet().iterator();
			while (it.hasNext()) {
				Computer  c= it.next().getValue();
				result.add(rightPad(c.getIp(), 16)+GAP+rightPad(c.getName(), 16)+GAP+rightPad(c.getTag(), 16));
			}
		}
		result.add("");
		result.add("");
		
		result.add("#服务配置列表");
		
		result.add("");
		result.add("##BS服务配置列表");
		result.add("<服务名称          监听IP           监听端口             前端IP          前端端口             前端IP          前端端口             是否支持Xlsx>");
		Map<Integer, NginxService> servers = userset.getServiceList().getServiceBs();
		if (null!= servers) {
			Iterator<Entry<Integer, NginxService>> it = servers.entrySet().iterator();
			while (it.hasNext()) {
				NginxService n = it.next().getValue();
				result.add(rightPad(n.getServerName(), 16)+GAP
					+ rightPad(userset.getComputerIpById(n.getListenId()), 16) + GAP + rightPad(n.getListenPort(), 16) + GAP
					+ rightPad(userset.getComputerIpById(n.getFontEndId()), 16) + GAP + rightPad(n.getFontEndPort(), 16) + GAP
					+ rightPad(userset.getComputerIpById(n.getBackEndId()), 16) + GAP + rightPad(n.getBackEndPort(), 16) + GAP
					+ ((null!=n.getSupportXLSX() && n.getSupportXLSX())? "支持" : "不支持"));
			}
		}
		result.add("");
		result.add("##CS服务配置列表");
		result.add("<服务名称          监听IP           监听端口             前端IP          前端端口             前端IP          前端端口             是否支持Xlsx>");
		return result;
	}
	
	private String rightPad(int portSelf, int length) {
		return rightPad(String.valueOf(portSelf), length);
	}

	private String rightPad(String string,int length) {
		StringBuilder result = new StringBuilder();
		if (null != string) {
			result.append(string);
			length = length - result.length();
		}
		if (length> 0) {
			for (int i = 0; i < length; i++) {
				result.append(GAP);
			}
		}
		return result.toString();
	}
	
	public List<String> getContent(boolean isBs){
		List<String> list = new ArrayList<>();
		list.add("worker_processes  1;");
		list.add("");
		list.add("events {");
		list.add("    worker_connections  1024;");
		list.add("}");
		list.add("");
		list.add("http {");
		list.add("    include       mime.types;");
		list.add("    default_type  application/octet-stream;");
		list.add("");
		list.add("    client_max_body_size 10M;");
		list.add("");
		list.add("    sendfile        on;");
		list.add("");
		list.add("    keepalive_timeout  65;");
		list.add("");
		Map<Integer, NginxService> services = isBs?userset.getServiceList().getServiceBs():userset.getServiceList().getServiceCs();
		if (null!= services) {
			for (Entry<Integer, NginxService> entry : services.entrySet()) {
				if (null != entry.getValue())
				list = addBody(list, entry.getValue());
			}
		}
		list.add("");
		list.add("}");
		return list;
	}
	
	List<String> addBody(List<String> list,NginxService server){
		if (null!= list && null!= server) {
			Computer fontEnd = userset.getComputerById(server.getFontEndId());
			Computer backEnd = userset.getComputerById(server.getBackEndId());
			
			list.add("    # "+server.getServerName()+" "+ fontEnd.getName()+"-"+(backEnd.getId()<=0?"Myself":backEnd.getName()));
			list.add("    server {");
			list.add("        listen       "+server.getListenPort()+";");
			list.add("        server_name  "+userset.getComputerById(server.getListenId()).getIp()+";");
			list.add("");
			list.add("        location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf|js|css|html|json"+((null!=server.getSupportXLSX() && server.getSupportXLSX())?"|xlsx":"")+")$");
			list.add("        {");
			list.add("");
			list.add("            proxy_set_header  Host $host;");
			list.add("            proxy_set_header  X-Real-IP  $remote_addr;");
			list.add("            proxy_set_header X-Forwarded-For $remote_addr;");
			list.add("            proxy_pass http://"+fontEnd.getIp()+":"+server.getFontEndPort()+";");
			list.add("        }");
			list.add("");
			list.add("        location / {");
			list.add("           proxy_set_header Host $host;");
			list.add("           proxy_set_header X-Real-Ip $remote_addr;");
			list.add("           proxy_set_header X-Forwarded-For $remote_addr;");
			list.add("           proxy_pass http://"+backEnd.getIp()+":"+server.getBackEndPort()+";");
			list.add("        }");
			list.add("");
			list.add("        error_page   500 502 503 504  /50x.html;");
			list.add("        location = /50x.html {");
			list.add("            root   html;");
			list.add("        }");
			list.add("");
			list.add("    }");
		}
		return list;
	}
}
